package com.steadyoung.modelapp.base;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.steadyoung.modelapp.R;
import com.steadyoung.modelapp.util.statusbar.StatusBarCompat;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import butterknife.ButterKnife;


/**
 * Created by Administrator on 2017/9/7.
 * Email : steadyoung@foxmail.com
 * Auth  : wayne
 * Desc  : Activity基类
 */
public abstract class BaseActivity extends RxAppCompatActivity {

    /***
     * 状态栏默认颜色
     */
    public static final int DEFAULT_COLOR =
//            Color.parseColor("#333333");
            Color.parseColor("#3A3B3D");

    /**设置布局ID*/
    protected abstract int getLayoutResId();
    /**初始化数据*/
    protected abstract void initData();
    /**初始化事件监控*/
    protected abstract void initEvent();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("BaseActivity", "onCreate: " + getClass().getSimpleName());
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(getLayoutResId());
        ButterKnife.bind(this);
        StatusBarCompat.setStatusBarColor(this, DEFAULT_COLOR);
        initData();
        initEvent();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("BaseActivity", "onDestroy: "  + getClass().getSimpleName());
    }

    public void onLeftClick(View v){
        onBackPressed();
    }

    public void onRightClick(View v){

    }

    public void onCenterClick(View v){

    }

    /**
     * 设置左侧图片
     * @param res
     */
    protected void setLeftIcon(int res){
        if(findViewById(R.id.title_left_tv) !=null ){
            setDrawableLeft((TextView) findViewById(R.id.title_left_tv),res);
        }
    }

    /**
     * 设置右侧图片
     * @param res
     */
    protected void setRightIcon(int res){
        if(findViewById(R.id.title_right_tv) !=null ){
            setDrawableRight((TextView) findViewById(R.id.title_right_tv),res);
        }
    }

    /**
     * 设置中间图片
     * @param res
     */
    protected void setCenterIcon(int res){
        if(findViewById(R.id.title_center_tv) !=null ){
            setDrawableLeft((TextView) findViewById(R.id.title_center_tv),res);
        }
    }

    /**
     * 设置左侧文字
     * @param res
     */
    protected void setLeftText(int res){
        setText(R.id.title_left_tv,res);
    }

    /**
     * 设置右侧文字
     * @param res
     */
    protected void setRightText(int res){
        setText(R.id.title_right_tv,res);
    }

    /**
     * 设置中间文字
     * @param res
     */
    protected void setCenterText(int res){
        setText(R.id.title_center_tv,res);
    }

    /**
     * 设置中间文字
     * @param text
     */
    protected void setCenterText(String text){
        setText(R.id.title_center_tv,text);
    }

    /**
     * 设置textview 的drawable属性(左侧图片)
     *
     * @param attention
     * @param drawableRes 左侧图片
     */
    private void setDrawableLeft(TextView attention, int drawableRes) {
        Drawable drawable = getResources().getDrawable(drawableRes);
        attention.setVisibility(View.VISIBLE);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        attention.setCompoundDrawables(drawable, null, null, null);
    }

    /**
     * 设置textview 的drawable属性（右侧图片）
     *
     * @param attention
     * @param drawableRes 右侧图片
     */
    private void setDrawableRight(TextView attention, int drawableRes) {
        Drawable drawable = getResources().getDrawable(drawableRes);
        attention.setVisibility(View.VISIBLE);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        attention.setCompoundDrawables(null, null, drawable, null);
    }

    private void setText(int viewRes,int textRes){
        if(findViewById(viewRes) !=null ){
            TextView tv = (TextView) findViewById(viewRes);
            tv.setVisibility(View.VISIBLE);
            tv .setText(textRes);
        }
    }

    private void setText(int viewRes,String text){
        if(findViewById(viewRes) !=null ){
            TextView tv = (TextView) findViewById(viewRes);
            tv.setVisibility(View.VISIBLE);
            tv .setText(text);
        }
    }

}
